<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客系统</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <link rel="icon" href="pict/login.png" sizes="16x16">
    <script src="js1/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <div class="personlog">
            <img src="images/others/head_new.jpg" alt="">
            <ul class="log_new">
                <li><a href="index.html">个人中心</a></li>
            </ul>
        </div>
        <span class="title">博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer">
             <span class="search">
        <input type="text" class="in"  id="in" placeholder="  请输入查询内容" />
            <button class="btn_search" onclick="showInput()">搜索</button>
    </span>
        </span>
        <a href="index1.html">主页</a>
        <a href="blog_write.html">写博客</a>
        <a href="explain.html">博客说明</a>
        <a id="islogin" href="javascript:logout()">退出</a>
    </div>


    <!--版心-->
    <div class="container">
        <div class="container-right">
            <div id="alist">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->
            </div>
            <div id="page" style="text-align:center;padding-top:50px;padding-bottom: 30px;">
                <a href="javascript:lastPage();" class="a_pre">上一页</a>
                <a href="javascript:nextPage();" class="a_next">下一页</a>
            </div>
        </div>
    </div>

    <script>
        function showInput(){
            var val = $("#in").val();
            jQuery.ajax({
                url:"google",
                type:"POST",
                data:{"val":val},
                success:function(result){
                    if(result != 0){
                        if(result!=null && result != ""){//查询到了文章
                            var artListHtml="";
                            for(var i=0;i<result.length;i++){
                                var article = result[i];
                                artListHtml += '<div class="blog"><div class="title">'+article.title+'</div><div class="date">'+article.createtime+'</div><div class="desc">'+article.context.substring(0,10)  + "..." +'</div><a href="blog_content.html?aid='+article.aid+'" class="detail">查看全文 &gt;&gt;</a></div><hr>';
                            }
                            jQuery("#alist").html(artListHtml);

                        }else{
                            jQuery("#alist").html("<h2 style='text-align:center;padding-top:50px;padding-bottom: 30px;'>暂无文章,<a href=index1.html>返回</h2>")
                            jQuery("#page").hide();
                        }
                    }else {
                        alert("无数据");
                    }
                }
            });
        }

        function myUserInfo() {
            jQuery.ajax({
                url: "loginUserInfo",
                type: "POST",
                data: "",
                success: function (result) {
                    if (result != null) {
                        if (result.id != null) {

                        } else {
                            alert("抱歉，请先登录！");
                            location.href="login.jsp";
                        }
                    } else {
                        alert("抱歉！后端程序不可用，请您稍后再试！?");
                    }
                }
            });
        };
        myUserInfo();



        //获取当前url中某个参数的方法
        function getURLParam(key){
            var params=location.search;
            if(params.indexOf("?")>=0){
                params=params.substring(1).split("&");
                var paramArr=params;
                for(var i=0;i<paramArr.length;i++){
                    var namesvalues=paramArr[i].split("=");
                    if(namesvalues[0]==key){
                        return namesvalues[1];
                    }
                }
            }else{
                return "";
            }
        }

        var currPage=getURLParam("cp")==""?1:getURLParam("cp");//当前页码
        var pageSize=getURLParam("ps")==""?3:getURLParam("ps");//每页显示的最大条数

        function lastPage(){
            //上一页
            if(currPage>1){
                currPage--;
            }else{
                alert("当前已经在首页");
                return;
            }
            location.href="index1.html?cp="+currPage+"&ps="+pageSize;
        }

        function nextPage(){
            //下一页
            currPage++;
            location.href="index1.html?cp="+currPage+"&ps="+pageSize;

        }
        //请求后端查询列表
        function loadList(){
            jQuery.ajax({
                url:"artlist",
                type:"POST",
                data:{"cp":currPage,"ps":pageSize},
                success:function(result){
                    if(result!=null && result != ""){//查询到了文章
                        var artListHtml="";
                        for(var i=0;i<result.length;i++){
                            var article = result[i];
                            artListHtml += '<div class="blog"><div class="title">'+article.title+'</div><div class="date">'+article.createtime+'</div><div class="desc">'+article.context.substring(0,10)  + "..." +'</div><a href="blog_content.html?aid='+article.aid+'" class="detail">查看全文 &gt;&gt;</a></div><hr>';

                        }
                        jQuery("#alist").html(artListHtml);

                    }else{
                        jQuery("#alist").html("<h2 style='text-align:center;padding-top:50px;padding-bottom: 30px;'>暂无文章,<a href=index1.html>返回</h2>")
                        jQuery("#page").hide();
                    }
                }
            });
        }
        loadList();

        //退出事件
        function logout() {
            if (confirm("是否退出？")) {
                //1.session清空
                jQuery.ajax({
                    url: "loginout",
                    type: "POST",
                    success: function (result) {
                        if (result != null && result == 1) {
                            alert("退出成功！");
                            location.href = "index.html";
                        } else {
                            alert("抱歉，操作出错，请重试！");
                        }
                    }
                });

            }
        }
    </script>



</body>

</html>